<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Auth\Authenticatable;
use App\MongoBaseModel as Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Laravel\Passport\HasApiTokens;

class User extends Model implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword, Notifiable, HasApiTokens;

/**
 * Class User
 * @package App\Models
 * @property string $id
 * @property string $name
 * @property string $group_id
 * @property string $company_id
 * @property string $password
 * @property string $created_at
 * @property string $updated_at
 **/
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token','_id', 'created_at', 'updated_at'
    ];
    // 后期改为用belongsTo关系来写, 先完成功能
    protected $appends = ['id'];
    protected $dates = ['deleted_at'];

    // user和project是多对多关系
    public function project() {
        return $this->belongsToMany(Project::class);
    }

    public function getCompaniesAttribute() {
        $companyId = $this->getAttribute('company_id');
        //dd($companyId);
        if (!empty($companyId)) {
            $company = Company::find($companyId);
            if (!is_null($company)) {
                return $company->name;
            }
        }else {
            return "";
        }
    }

    public function getDisabledAttribute() {
        if (isset($this->deleted_at) && $this->delete()) {
            return true;
        } else {
            return false;
        }
    }

    //管理员
//    const GROUP_ROLE_ADMIN = 2;
    //操作员
//    const GROUP_ROLE_NOR = 3;

    public function scopeSearch($query, $inputs){
        if(!empty($inputs)){
            return $query->where(function ($querys) use ($inputs) {
                if (!empty($inputs['email'])) {
                    $querys->where('email', 'like', "%{$inputs['email']}%");
                }
                if (!empty($inputs['name'])) {
                    $querys->where('name', 'like', "%{$inputs['name']}%");
                }
                if (!empty($inputs['mobile'])) {
                    $querys->where('mobile', $inputs['mobile']);
                }
            });
        }
    }

    /**
     * 对用户所属外键关系的绑定 - Group
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function group() {

        return $this->belongsTo(Group::class);
    }

    /**
     * 对用户所属外键关系的绑定 - Company
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function company() {
        return $this->belongsTo(Company::class);
    }

    public function getGroupAttribute() {
        return $this->group();
    }

    public function getWxClientAttribute() {
        // TODO 通过当前用户负责的Project,查询绑定的微信客户端信息并返回
    }
}
